/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmUsuarios.java
 *
 * Created on 3/11/2011, 12:19:34 PM
 */

package Usuarios;

import Beans.UsuarioBean;
import Blogic.UsuarioBLogic;
import Menus.frmMenus;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author orellana
 */
public class frmUsuarios extends javax.swing.JFrame {

    /** Creates new form frmUsuarios */
    public frmUsuarios() {
        initComponents();
        // Quitamos el Resaized
        this.setResizable(false);
        // Centrado de Pantalla
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnAccesar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();

        jPopupMenu1.setName("jPopupMenu1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sistemapegasso.SistemaPegassoApp.class).getContext().getResourceMap(frmUsuarios.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(resourceMap.getColor("Form.foreground")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jPanel3.setBackground(resourceMap.getColor("jPanel3.background")); // NOI18N
        jPanel3.setBorder(null);
        jPanel3.setName("jPanel3"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        btnAccesar.setBackground(resourceMap.getColor("btnAccesar.background")); // NOI18N
        btnAccesar.setFont(resourceMap.getFont("btnAccesar.font")); // NOI18N
        btnAccesar.setIcon(resourceMap.getIcon("btnAccesar.icon")); // NOI18N
        btnAccesar.setText(resourceMap.getString("btnAccesar.text")); // NOI18N
        btnAccesar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btnAccesar.setFocusable(false);
        btnAccesar.setName("btnAccesar"); // NOI18N
        btnAccesar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAccesarActionPerformed(evt);
            }
        });
        btnAccesar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnAccesarKeyPressed(evt);
            }
        });

        jButton2.setBackground(resourceMap.getColor("jButton2.background")); // NOI18N
        jButton2.setFont(resourceMap.getFont("jButton2.font")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jButton2.setFocusable(false);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setIcon(resourceMap.getIcon("jLabel2.icon")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        txtUsuario.setText(resourceMap.getString("txtUsuario.text")); // NOI18N
        txtUsuario.setName("txtUsuario"); // NOI18N
        txtUsuario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtUsuarioFocusLost(evt);
            }
        });

        txtPassword.setText(resourceMap.getString("txtPassword.text")); // NOI18N
        txtPassword.setName("txtPassword"); // NOI18N
        txtPassword.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPasswordFocusLost(evt);
            }
        });
        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtPasswordKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnAccesar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtPassword)
                            .addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(btnAccesar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAccesarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAccesarActionPerformed
        // TODO add your handling code here:
        // Variables Locales
        UsuarioBean sqlBean=new UsuarioBean();
        Vector datosUsuario=new Vector();
        frmMenus principal=null;

        // Mensaje en Consola
        System.out.println("Presione el Boton de Accesar");
        // Evaluamos Autenticacion
        if (this.txtUsuario.getText().equals("")){
            // Mensaje al Usuario
            JOptionPane.showMessageDialog(null,"Capture el Usuario, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
        }else if (new String(this.txtPassword.getPassword()).equals("")){
            // Mensaje al Usuario
            JOptionPane.showMessageDialog(null,"Capture la Contraseña, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
        }else{
            // Evaluamos si existe el Usuario
            // Seteamos los Datos
            sqlBean.setUsuario(this.txtUsuario.getText());
            sqlBean.setPassword(new String(this.txtPassword.getPassword()));
            // Realizamos la Consulta
            datosUsuario=(new UsuarioBLogic()).execute(sqlBean);

            // Realizamos Operaciones
            try {
                // Vaciamos el Resultado Obtenido
                sqlBean=(UsuarioBean) datosUsuario.elementAt(0);

                // Evaluamos los Datos Obtenidos
                // Usuario
                if (this.txtUsuario.getText().equals(sqlBean.getUsuario().toUpperCase())){
                    // Password
                    if (new String(this.txtPassword.getPassword()).toUpperCase().equals(sqlBean.getPassword())){
                        // Abrimos la Pantalla Principal
                        principal=new frmMenus();
                        principal.setVisible(true);
                        // Cerramos la Pantalla de Autentificacion
                        this.setVisible(false);
                    }else{
                        // Mensaje al Usuario
                        JOptionPane.showMessageDialog(null,"Contraseña Incorrecta, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
                        // Limpiamos los Campos
                        this.txtPassword.setText("");
                        // Movemos el Foco
                        this.txtPassword.requestFocus();
                    }
                }else{
                    // Mensaje al Usuario
                    JOptionPane.showMessageDialog(null,"Usuario Incorrecto, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
                    // Limpiamos los Campos
                    this.txtUsuario.setText("");
                    this.txtPassword.setText("");
                    // Movemos el Foco
                    this.txtUsuario.requestFocus();
                }
            } catch (Exception e) {
                // Mensaje al Usuario
                JOptionPane.showMessageDialog(null,"Usuario no Registrado, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
                // Limpiamos los Campos
                this.txtUsuario.setText("");
                this.txtPassword.setText("");
                // Movemos el Foco
                this.txtUsuario.requestFocus();
            }
        }
    }//GEN-LAST:event_btnAccesarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        // Finalizamos el Programa
        System.exit(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void txtUsuarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUsuarioFocusLost
        // TODO add your handling code here:
        txtUsuario.setText(txtUsuario.getText().toUpperCase());
    }//GEN-LAST:event_txtUsuarioFocusLost

    private void txtPasswordFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswordFocusLost
        // TODO add your handling code here:
        txtPassword.setText(new String(txtPassword.getPassword()).toUpperCase());
    }//GEN-LAST:event_txtPasswordFocusLost

    private void btnAccesarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnAccesarKeyPressed
        // TODO add your handling code here:
         // Variables Locales
        UsuarioBean sqlBean=new UsuarioBean();
        Vector datosUsuario=new Vector();
        frmMenus principal=null;

        // Mensaje en Consola
        System.out.println("Presione el Boton de Accesar");
        // Evaluamos Autenticacion
        if (this.txtUsuario.getText().equals("")){
            // Mensaje al Usuario
            JOptionPane.showMessageDialog(null,"Capture el Usuario, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
        }else if (new String(this.txtPassword.getPassword()).equals("")){
            // Mensaje al Usuario
            JOptionPane.showMessageDialog(null,"Capture la Contraseña, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
        }else{
            // Evaluamos si existe el Usuario
            // Seteamos los Datos
            sqlBean.setUsuario(this.txtUsuario.getText());
            sqlBean.setPassword(new String(this.txtPassword.getPassword()));
            // Realizamos la Consulta
            datosUsuario=(new UsuarioBLogic()).execute(sqlBean);

            // Realizamos Operaciones
            try {
                // Vaciamos el Resultado Obtenido
                sqlBean=(UsuarioBean) datosUsuario.elementAt(0);

                // Evaluamos los Datos Obtenidos
                // Usuario
                if (this.txtUsuario.getText().equals(sqlBean.getUsuario())){
                    // Password
                    if (new String(this.txtPassword.getPassword()).equals(sqlBean.getPassword())){
                        // Abrimos la Pantalla Principal
                        principal=new frmMenus();
                        principal.setVisible(true);
                        // Cerramos la Pantalla de Autentificacion
                        this.setVisible(false);
                    }else{
                        // Mensaje al Usuario
                        JOptionPane.showMessageDialog(null,"Contraseña Incorrecta, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
                        // Limpiamos los Campos
                        this.txtPassword.setText("");
                        // Movemos el Foco
                        this.txtPassword.requestFocus();
                    }
                }else{
                    // Mensaje al Usuario
                    JOptionPane.showMessageDialog(null,"Usuario Incorrecto, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
                    // Limpiamos los Campos
                    this.txtUsuario.setText("");
                    this.txtPassword.setText("");
                    // Movemos el Foco
                    this.txtUsuario.requestFocus();
                }
            } catch (Exception e) {
                // Mensaje al Usuario
                JOptionPane.showMessageDialog(null,"Usuario no Registrado, Verifique","Validacion",JOptionPane.INFORMATION_MESSAGE);
                // Limpiamos los Campos
                this.txtUsuario.setText("");
                this.txtPassword.setText("");
                // Movemos el Foco
                this.txtUsuario.requestFocus();
            }
        }
    }//GEN-LAST:event_btnAccesarKeyPressed

    private void txtPasswordKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyReleased
        // TODO add your handling code here:
        // Tecla [Enter]
        if (evt.getKeyCode()==10){
            // LLamamos al Boton de Aceptar
            btnAccesar.doClick();
        }
    }//GEN-LAST:event_txtPasswordKeyReleased

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {  // Atributos de la Clase
                new frmUsuarios().setVisible(true);
                
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAccesar;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables

}
